#include "Score.h"
//#include "Global.h"
#include "Resource.h"

Score::Score(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y) : Object(x, y)
{
	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\Score.png"), 0, 0);
	
	score = 0;

	_xOld = _x;
	_yOld = _y;

	_vx = 0;
	_vy = -1.5;//-200;
	_accelY = GRAVITY;

	object_State = IS_MOVING;
}

Score::~Score()
{
	/*if (_sprite != NULL)
	{
		delete _sprite;
	}*/
}

 

void Score::Render(RECT camera)
{
	DrawNumber(_sprite, score, camera.left + _x, _y);
}

void Score::Render()
{
	DrawNumber(_sprite, score, _x, _y);
}

OBJECT_TYPE Score::GetTypeObject()
{
	return OBJECT_LAND;
}

void Score::ResetRect()
{
	_rectBond.left = _x;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _y;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
}

void Score::Update(list<Object*>* staObjs, list<Object*>* dynObjs, DxInput* input, float gameTime)
{
	  
	_vy += _accelY * gameTime;
	_y += _vy * gameTime + 1/2 * _accelY * gameTime * gameTime;
	if (_vy >= 0)//_y > _yOld)
	{
		ChangeState(IS_IDLE);
	}
	_sprite->Update(gameTime);
	ResetRect();
	 
}

void Score::DrawNumber(Sprite* sprite,int number,int x,int y)
{
	RECT rSrc;
	int numWidth=sprite->texture->_Width;///10;
	do
	{
		int temp=number%10;
		number=number/10;
		sprite->_Index = temp;
		/*rSrc.left=numWidth*temp;
		rSrc.right=numWidth*(temp+1);
		rSrc.top=0;
		rSrc.bottom=sprite->texture->_Height;*/
		//sprite->Draw(x,y,zoomX,zoomY,rSrc,0.1);
		sprite->Render(x,y);
		x = x - numWidth;
	}while(number>0);
}

void Score::Update(int Score)
{
	 
	this->score = Score;
}

void Score::ChangeState(int state)
{
	object_State = state;
	switch(object_State)
	{
	
	case IS_MOVING:
		_vy = -2.0;
		_accelY = GRAVITY;
		break;
	case IS_IDLE:
		break;
	}
}